package com.jingdianjichi.auth.application.controller;

import com.jingdianjichi.auth.application.converter.AuthRoleDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthRoleDTO;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthRoleBO;
import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/*
* 角色Controller
* */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /*
    * 新增角色
    * */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("RoleController.add.authRoleDTO:{}",authRoleDTO);
            }
            Objects.requireNonNull(authRoleDTO.getRoleName(),"角色名字不能为空");
            Objects.requireNonNull(authRoleDTO.getRoleKey(),"角色的唯一标识不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.Instance.DTOConverterBO(authRoleDTO);
            Boolean result = authRoleDomainService.add(authRoleBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("RoleController.add.authRoleDTO:{}",e.getMessage(),e);
            return Result.fail("新增角色失败");
        }
    }

    /*
    * 修改角色
    * */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("RoleController.update.authRoleDTO:{}",authRoleDTO);
            }
            Objects.requireNonNull(authRoleDTO.getId(),"角色的Id不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.Instance.DTOConverterBO(authRoleDTO);
            Boolean result = authRoleDomainService.update(authRoleBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("RoleController.update.authRoleDTO:{}",e.getMessage(),e);
            return Result.fail("修改角色失败");
        }
    }

    /*
    * 删除角色
    * */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("RoleController.delete.authRoleDTO:{}",authRoleDTO);
            }
            Objects.requireNonNull(authRoleDTO.getId(),"角色的Id不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.Instance.DTOConverterBO(authRoleDTO);
            Boolean result = authRoleDomainService.delete(authRoleBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("RoleController.delete.authRoleDTO:{}",e.getMessage(),e);
            return Result.fail("删除角色失败");
        }
    }
}
